//
//  ADCommonMethods.m
//  ADYiZhiYu
//
//  Created by 晓 on 2017/11/15.
//  Copyright © 2017年 com.idolmedia. All rights reserved.
//

#import "ADCommonMethods.h"

@implementation ADCommonMethods

+ (ADCommonMethods *)sharedCommonMethods
{
    static ADCommonMethods *sharedAccountManagerInstance = nil;
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        sharedAccountManagerInstance = [[self alloc] init];
    });
    return sharedAccountManagerInstance;
}

//邮箱
+ (BOOL)validateEmail:(NSString *)email
{
    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:email];
}


//控制输入一定长度的字符串
+ (BOOL)validateString:(NSString*)number range:(NSRange)range limitLength:(NSInteger)limitLength{
    BOOL res = YES;
    NSCharacterSet *tmpSet;
    tmpSet = [NSCharacterSet characterSetWithCharactersInString:@"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'[]\\{}#%^*+=.,?!_|~<>€£¥•-/:;()$&@\""];
    
    int i = 0;
    while (i < number.length)  {
        NSString * string = [number substringWithRange:NSMakeRange(i, 1)];
        NSRange tmpRange = [string rangeOfCharacterFromSet:tmpSet];
        if (tmpRange.length == 0) {
            res = NO;
            break;
        }
        else
        {
            if (range.location >= limitLength) {
                res = NO;
            }
            else
            {
                res = YES;
            }
        }
        i++;
    }
    return res;
}

//控制输入电话号码
+ (BOOL)validatePhoneNumber:(NSString*)number range:(NSRange)range{
    BOOL res = YES;
    NSCharacterSet *tmpSet;
    if (range.location == 0) {
        tmpSet = [NSCharacterSet characterSetWithCharactersInString:@"1"];
    }
    else if(range.location == 1)
    {
        tmpSet = [NSCharacterSet characterSetWithCharactersInString:@"34578"];
    }
    else
    {
        tmpSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
    }
    
    
    int i = 0;
    while (i < number.length)  {
        NSString * string = [number substringWithRange:NSMakeRange(i, 1)];
        NSRange tmpRange = [string rangeOfCharacterFromSet:tmpSet];
        if (tmpRange.length == 0) {
            res = NO;
            break;
        }
        else
        {
            if (range.location >= 11) {
                res = NO;
            }
            else
            {
                res = YES;
            }
        }
        i++;
    }
    return res;
}
//检测字符串是否为空
+ (BOOL)isBlankString:(NSString *)string{
    
    if (string == nil) {
        return YES;
    }
    if (string == NULL) {
        return YES;
    }
    if ([string isKindOfClass:[NSNull class]]) {
        return YES;
    }
    if ([string isEqualToString:@"(null)"]) {
        return YES;
    }
    if ([string isEqualToString:@"<null>"]) {
        return YES;
    }
    if ([string isEqualToString:@""]) {
        return YES;
    }
    if ([[string stringByTrimmingCharactersInSet:
          [NSCharacterSet whitespaceCharacterSet]] length]==0) {
        return YES;
    }
    return NO;
}

//判断是否只含纯数字
+ (BOOL)judgeTheillegalNumber:(NSString *)content{
    
    NSString *regex =@"[0-9]*";
    NSPredicate* pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
    if (![pred evaluateWithObject:content]) {
        return YES;
    }
    return NO;
}

//获取当前时间
+ (NSString *)getNowdataStr{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    [formatter setTimeZone:[NSTimeZone timeZoneWithName:@"Asia/Shanghai"]];
    //转化为东八区的时间，即北京时间
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    NSString *DateTime = [formatter stringFromDate:[NSDate date]];
    return DateTime;
}

//获取当天时间
+ (NSString *)getCurrentDayStr{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"YYYY-MM-dd"];
    [formatter setTimeZone:[NSTimeZone timeZoneWithName:@"Asia/Shanghai"]];
    //转化为东八区的时间，即北京时间
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"YYYY-MM-dd"];
    NSString *DateTime = [formatter stringFromDate:[NSDate date]];
    return DateTime;
}


//计算时间差（转换成秒）倒计时15分钟
+ (NSInteger)calculateTimeChaWithOrderCreatTime:(NSString *)creatTimeStr withFailureTimeStr:(NSString *)failureTimeStr withCurrentTimeStr:(NSString *)currentTimeStr{
    NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init];
    [dateformatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    NSDate *currentTime = [dateformatter dateFromString:currentTimeStr];
    NSDate *creatTime = [dateformatter dateFromString:creatTimeStr];
    NSTimeInterval timeDateCha = [currentTime timeIntervalSinceDate:creatTime];
    NSInteger timeCha = timeDateCha;
    NSInteger failureTime = [failureTimeStr integerValue] * 60;
    NSInteger timeChaValue = failureTime - timeCha;
    return timeChaValue;
}

@end
